package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.mybatis.Pagination;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.work.WorkBO;
import com.ymk.core.code.cmd.WorkCmdCode;
import com.ymk.core.constants.WorkConstants;
import com.ymk.service.IWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class WorkListCmd extends ACmd {

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        WorkBO reqData = super.getData(sign, WorkBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("userId",reqData.getUserId())
                .addParam("state",reqData.getState())
                .addParam("workName",reqData.getWorkName())
                .addParam("interviewAddress",reqData.getInterviewAddress())
                .setReturnStruct(sign.getReturnStruct());
        if (Objects.nonNull(sign.getReqCode()) && WorkConstants.PRICE_HIGH_TO_DOWN.equals(sign.getReqCode())){
            paramsBuilder.addParam("priceHigh",true);
        }
        if (Objects.nonNull(sign.getReqCode()) && WorkConstants.PRICE_DOWN_TO_HIGH.equals(sign.getReqCode())){
            paramsBuilder.addParam("priceDown",true);
        }
        if (Objects.nonNull(sign.getReqCode()) && WorkConstants.DATE_FAR_TO_NEAR.equals(sign.getReqCode())){
            paramsBuilder.addParam("dateFar",true);
        }
        if (Objects.nonNull(sign.getReqCode()) && WorkConstants.DATE_NEAR_TO_FAR.equals(sign.getReqCode())){
            paramsBuilder.addParam("dateNear",true);
        }

        Pagination pagination = sign.buildPage();
        pagination.setParams(paramsBuilder.build());
        List<WorkBO> workList = this.workService.listWorkIdForPage(pagination);
        if (!workList.isEmpty()){
            List<Integer> workIdList = workList.stream().map( WorkBO::getId ).collect(Collectors.toList());
            paramsBuilder.clearParam()
                    .addParam("workIdList",workIdList)
                    .addParam("loadWorkImg",true)
                    .addParam("loadWorkType",true);
            List<WorkBO> respDataList = this.workService.listForPage(paramsBuilder.build());

            respSign.setSource(super.getSource(respDataList));
        }

        respSign.setPageInfo(pagination);
        return respSign;
    }

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        WorkBO reqData = super.getData(sign, WorkBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("state",reqData.getState())
                .addParam("workName",reqData.getWorkName())
                .addParam("interviewAddress",reqData.getInterviewAddress())
                .setReturnStruct(sign.getReturnStruct());
        if (Objects.nonNull(sign.getReqCode()) && WorkConstants.PRICE_HIGH_TO_DOWN.equals(sign.getReqCode())){
            paramsBuilder.addParam("priceHigh",true);
        }
        if (Objects.nonNull(sign.getReqCode()) && WorkConstants.PRICE_DOWN_TO_HIGH.equals(sign.getReqCode())){
            paramsBuilder.addParam("priceDown",true);
        }
        if (Objects.nonNull(sign.getReqCode()) && WorkConstants.DATE_FAR_TO_NEAR.equals(sign.getReqCode())){
            paramsBuilder.addParam("dateFar",true);
        }
        if (Objects.nonNull(sign.getReqCode()) && WorkConstants.DATE_NEAR_TO_FAR.equals(sign.getReqCode())){
            paramsBuilder.addParam("dateNear",true);
        }

        Pagination pagination = sign.buildPage();
        pagination.setParams(paramsBuilder.build());
        List<WorkBO> workList = this.workService.listWorkIdForPage(pagination);
        if (!workList.isEmpty()){
            List<Integer> workIdList = workList.stream().map( WorkBO::getId ).collect(Collectors.toList());
            paramsBuilder.clearParam()
                    .addParam("workIdList",workIdList)
                    .addParam("loadWorkImg",true)
                    .addParam("loadWorkType",true);
            List<WorkBO> respDataList = this.workService.listForPage(paramsBuilder.build());

            respSign.setSource(super.getSource(respDataList));
        }

        respSign.setPageInfo(pagination);

        return respSign;
    }

    @Override
    public String getCmdCode() {
        return WorkCmdCode.WORK_LIST;
    }

    @Override
    public boolean anon(CmdSign sign) {
        return true;
    }

    @Autowired
    private IWorkService workService;
}